#!/usr/bin/env python

from subprocess import *
import random
import time

def run_t(cmd, fn, n, a): 

	s = ''
	s += '%d %d\n' % (n, n)
	for i in range(n): 
		for j in range(n): 
			s += '%d ' % a[i*n+j]; 
		s += '\n'
	fd = open(fn, "w+"); 
	fd.write(s); 
	fd.close()
	t0 = time.time()
	p = Popen([cmd], shell=True, bufsize=4096,
	          stdin=PIPE, stdout=PIPE, stderr=PIPE)

	od, ed = p.communicate(s)
	t1 = time.time()
	td = t1 - t0; 
	print 'out: %s, time: %.3f' % (od, td)
	return od, ed

def test(): 
	n = 10
	a = []
	for i in range(n*n): 
		a.append(i+1)

	random.shuffle(a)

	od1, ed1 = run_t("luogu_p1434.exe", "a.txt", n, a)
	od2, ed2 = run_t("luogu_p1434_other.exe", "b.txt", n, a)
	return od1==od2

def main(): 
	for i in range(100): 
		print '\ntest %d' % i
		res = test()
		if not res:
			break

if __name__=="__main__": 
	main()

